﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web;
using System.Xml;

namespace LuceneWrap.Web.Common
{
    public class FeedManager
    {

        public static List<FeedResult> GetFeeds()
        {
            var feeds = RetrieveFeeds();
            return MapFeeds(feeds);
        }


        private static SyndicationFeed RetrieveFeeds()
        {
            XmlReader reader = XmlReader.Create("http://weblogs.asp.net/scottgu/atom.aspx");
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            reader.Close();
            return feed;
        }

       
        private static List<FeedResult> MapFeeds(SyndicationFeed feed)
        {
            List<FeedResult> feedResults = new List<FeedResult>(feed.Items.Count());
            foreach (var feeds in feed.Items)
            {
                FeedResult feedResult = new FeedResult();
                feedResult.Id = feeds.Id;
                feedResult.Title = feeds.Title.Text;
                feedResult.Summary = ((TextSyndicationContent)feeds.Content).Text;
                feedResults.Add(feedResult);
            }

            return feedResults;
        }
    }

}